<?php
/**
 * $Horde:  $
 *
 * Copyright 2001-2006 Horde Project (http://www.horde.org/)
 *
 * See the enclosed file LICENSE for license information (ASL). If you
 * did not receive this file, see http://www.horde.org/licenses/asl.php.
 *
 * @author  Jon Parise <jon@horde.org>
 * @author  Luciano Ramalho <luciano@ramalho.org>
 * @since   Groo 1.0
 * @package Groo
 */

@define('GROO_BASE', dirname(__FILE__));
require_once GROO_BASE . '/lib/base.php';
require_once 'Horde/Prefs/CategoryManager.php';

/* Get the current action ID. */
$actionID = Util::getFormData('actionID');

/* Sort out the sorting values. */
if (Util::getFormData('sortby') !== null) {
    $prefs->setValue('sortby', Util::getFormData('sortby'));
}
if (Util::getFormData('sortdir') !== null) {
   $prefs->setValue('sortdir', Util::getFormData('sortdir'));
}

/* Get the full, sorted collection. */
$items = Groo::listItems($prefs->getValue('sortby'),
                          $prefs->getValue('sortdir'));

/* Page variables. */
$title = _("My Items");

switch ($actionID) {
case 'search_items':
    /* If we're searching, only list those items that match the search
     * result. */
    $pattern = Util::getFormData('search_pattern');
    $search_desc = (Util::getFormData('search_desc') == 'on');
    $search_body = (Util::getFormData('search_body') == 'on');

    if (!empty($pattern) && ($search_body || $search_desc)) {
        $pattern = '/' . preg_quote($pattern, '/') . '/i';
        $search_result = array();
        foreach ($items as $item_id => $item) {
            if (($search_desc && preg_match($pattern, $item['desc'])) ||
                ($search_body && preg_match($pattern, $item['body']))) {
                $search_result[$item_id] = $item;
            }
        }

        /* Reassign $items to the search result. */
        $items = $search_result;
        $title = _("Search Results");
    }
    break;
}

Horde::addScriptFile('httpclient.js', 'horde', true);
Horde::addScriptFile('tooltip.js', 'horde', true);
Horde::addScriptFile('tables.js', 'groo', true);
require GROO_TEMPLATES . '/common-header.inc';
require GROO_TEMPLATES . '/menu.inc';

// list display
require GROO_TEMPLATES . '/list/list.inc';

require $registry->get('templates', 'horde') . '/common-footer.inc';
